package com.ericsson.enap.modules.kpi.service.impl;

import com.ericsson.common.pojo.FTPInfoVO;
import com.ericsson.common.util.SFtpCarrierUtilNew;
import com.ericsson.enap.modules.kpi.entity.EnipMeasInfo;
import com.ericsson.enap.modules.kpi.entity.ImsFiveKpi;
import com.ericsson.enap.modules.kpi.service.EnipKpiFileProcess;
import org.slf4j.Logger;
import org.springframework.stereotype.Component;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.*;

/**
 * SMF类型网元指标处理
 */
@Component
public class KpiFileProcessSMFImpl implements EnipKpiFileProcess {

    @Override
    public File download(ImsFiveKpi imsFiveKpi, String localDir, Logger log) {
        String fileName="pm_job_epg-all.csv";
        log.info("===================================================  start download Files");
        FTPInfoVO ftpInfo = new FTPInfoVO();
        ftpInfo.setHost(imsFiveKpi.getIp4());
        ftpInfo.setUserName(imsFiveKpi.getUsername());
        ftpInfo.setPassword(imsFiveKpi.getPassword());
        ftpInfo.setPath(imsFiveKpi.getDownloadPath());
        ftpInfo.setPort(21);
        SFtpCarrierUtilNew sFtpCarrierUtilNew=new SFtpCarrierUtilNew(ftpInfo,log);
        sFtpCarrierUtilNew.connect();
        //localDir="D:\\opt\\Ericsson\\core\\VGCFIFSTATIS\\SMF\\57\\";
        boolean result=sFtpCarrierUtilNew.downloadVgcKpiFile(imsFiveKpi.getDownloadPath(),fileName,localDir,fileName);
        sFtpCarrierUtilNew.disconnect();
        if(result){
            return new File(localDir+fileName);
        }else {
            return null;
        }
    }

    @Override
    public List<File> download(ImsFiveKpi imsFiveKpi, String localDir, Logger log, Date date, int delay) {
        return null;
    }

    @Override
    public Map<String, List<File>> download(ImsFiveKpi imsFiveKpi, String localDir, Logger log, Date date) {
        return null;
    }

    @Override
    public List<EnipMeasInfo> analysis(List<File> files, Logger log) {
        return null;
    }

    /**
     * 解析文件
     * @param file
     * @param log
     * @return
     */
    public List<EnipMeasInfo> analysis(File file, Logger log){
        List<EnipMeasInfo> meansInfoList = new ArrayList<>();
        BufferedReader reader = null;
        String  strHead ="";
        String  strData ="";
        try {
            reader = new BufferedReader(new FileReader(file));
            String tempStr;
            while ((tempStr = reader.readLine()) != null) {
                if(tempStr.startsWith("Time")){
                    strHead = tempStr;
                }else{
                    strData = tempStr;
                }
            }
        } catch (IOException e) {
            log.error("KpiFileProcessSMFImpl"+e.getMessage());
        }  finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e1) {
                    log.error("KpiFileProcessSMFImpl"+e1.getMessage());
                }
            }
        }
        String regexSr = "\\|";
        String[] headArr = strHead.split(regexSr);
        String[] dataArr = strData.split(regexSr);
        HashMap<String,String> map = new HashMap();
        int size = headArr.length;
        String name= "";
        String value= "";
        for (int i = 1;i<size;i++){
            name= headArr[i];
            value= dataArr[i];
            if(name!=null &&!"".equals(name.trim())){
                if("".equals(value.trim())){
                    value = "0";
                }
                map.put(name,value);
            }
        }
        if(dataArr.length>0&&headArr.length>0) {
            EnipMeasInfo enipMeasInfo = new EnipMeasInfo();
            enipMeasInfo.setBeginTime(dataArr[0]);
            enipMeasInfo.setMeasTypeMap(map);
            meansInfoList.add(enipMeasInfo);
        }
        return meansInfoList;
    }

    @Override
    public boolean needUncompress() {
        return false;
    }

    @Override
    public NetType getNetType() {
        return NetType.smf;
    }
}
